package com.yh.csx.bsf.flow.core.util;

import com.yh.csx.bsf.core.base.Callable;
import com.yh.csx.bsf.core.util.ContextUtils;
import com.yh.csx.bsf.flow.core.FlowProperties;
import lombok.extern.slf4j.Slf4j;
import lombok.var;

/**
 * @author: chejiangyi
 * @version: 2019-11-05 11:35
 **/
@Slf4j
public class CommonUtil {
    public static boolean  isMobileDevice(String requestHeader){
        /**
         * android : 所有android设备
         * mac os : iphone ipad
         * windows phone:Nokia等windows系统的手机
         */
        String[] deviceArray = new String[]{"android","mac os","windows phone"};
        if(requestHeader == null)
        { return false;}
        requestHeader = requestHeader.toLowerCase();
        for(int i=0;i<deviceArray.length;i++){
            if(requestHeader.indexOf(deviceArray[i])>0){
                return true;
            }
        }
        return false;
    }

    public static void timeSpan(String msg,Callable.Action0 action0){
        timeSpan(msg,()->{action0.invoke();return 1;});
    }

    public static <T> T timeSpan(String msg,Callable.Func0<T> action0){
        if(!ContextUtils.getBean(FlowProperties.class,false).getTimespanEnabled())
        {
            return action0.invoke();
        }
        else {
            var b = System.currentTimeMillis();
            T t = action0.invoke();
            var e = System.currentTimeMillis();
            log.info(msg + " 耗时:" + (e - b) + "毫秒");
            return t;
        }
    }
}
